<template>
  <div class="container Login-logo">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      class="login-form"
      label-width="75px"
      label-position="left"
    >
      <h2 class="login-title">警务调度平台</h2>
      <el-form-item label="用户名 :" prop="account" class="form-group">
        <el-input v-model="form.account"></el-input>
      </el-form-item>
      <el-form-item label="密码 :" prop="password" class="form-group">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item class="form-group">
        <el-button type="primary" @click="submitForm('login')">登录</el-button>
        <el-button type="primary" @click="submitForm('register')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import Ws from '../main'
// import { useStore } from 'vuex'
export default {
  name: 'LoginRegister',
  data() {
    return {
      form: {
        account: '',
        password: ''
      },
      rules: {
        account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      url: 'src/assets/login-logo.jpg',
      message: ''
    }
  },
  methods: {
    submitForm(type) {
      const formRef = this.$refs.form
      formRef.validate((valid) => {
        if (valid) {
          if (type === 'login') {
            this.login()
          } else if (type === 'register') {
            alert(type)
            this.register()
          }
        } else {
          return false
        }
      })
    },
    async login() {
      const result = await axios.post('user/login', this.form)
      if (result.data.code == 200) {
        this.$store.commit('setUserAccount', this.form.account)
        this.$router.push('/HomePage')
        // console.log(result)
        sessionStorage.setItem('account', result.data.data[0].account)
        // console.log('account', sessionStorage.getItem('account'))
        console.log('11111111111')
        console.log(Ws)
        Ws.WsConnect(sessionStorage.getItem('account'), '111111')
      } else {
        this.message = '用户名或密码错误'
        alert('用户名或密码错误')
      }
      // console.log(result.data.code)
      //console.log(result)
    },
    register() {
      console.log('发送注册请求')
    }
  }
}
</script>
<style>
.container {
  width: 100vw;
  height: 100vh;
  padding: 0;
}

.Login-logo {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('src/assets/bg.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  filter: blur(0px);
}

.login-form {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  background-color: rgba(255, 255, 255, 0.85);
}

.login-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}
</style>
